package com.project.zsxd.server.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 */
public class DateUtil {


    public static final Logger LOG = LoggerFactory.getLogger(DateUtil.class);
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);

    /**
     * 根据指定时间格式转换日期
     * @param dateText
     * @param dateFormat
     * @return
     * @throws ParseException
     */
    public static Date parseDate(String dateText, String dateFormat) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        return format.parse(dateText);
    }

    /**
     * 使用默认yyyy-MM-dd格式转换日期
     * @param dateText
     * @return
     * @throws ParseException
     */
    public static Date parseDate(String dateText) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
        return format.parse(dateText);
    }

    /**
     * 使用默认yyyy-MM-dd HH:mm:ss格式转换时间
     * @param timeText
     * @return
     * @throws ParseException
     */
    public static Date parseTime(String timeText) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        return format.parse(timeText);
    }

    /**
     * 按指定格式格式化日期
     * @param date
     * @param dateFormat
     * @return
     */
    public static String formatDate(Date date, String dateFormat) {
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        return format.format(date);
    }

    /**
     * 按默认格式yyyy-MM-dd格式化日期
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
        return format.format(date);
    }

    /**
     * 按默认格式yyyy-MM-dd HH:mm:ss格式化时间
     * @param date
     * @return
     */
    public static String formatTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        return format.format(date);
    }

    public static String getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR) + "";
    }

    public static String getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1 + "";
    }

    public static String getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH) + "";
    }

    /**
     * 计算日期
     * @param date
     * @param field
     * @param value
     * @return
     */
    public static Date calDate(Date date, int field, int value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field, value);
        return calendar.getTime();
    }




    /**
     * 转换入睡时间
     * 今日入睡时间>=今日12点 && <次日12点
     * 入睡时间在次日时，需要将日期提前到今日，且小时加24，便于前段处理后展示
     * @param date
     * @return
     */
    public static String convertSleepTime(Date date) {
        if (date == null) {
            return null;
        }
        String dateString = DATE_FORMAT.format(date);
        Calendar dateCal = Calendar.getInstance();
        dateCal.setTime(date);
        int hour = dateCal.get(Calendar.HOUR_OF_DAY);
        // 入睡时间的小时<12，说明是在次日0点至12点入睡的
        if (hour < 12) {
            Calendar resultCal = (Calendar) dateCal.clone();
            resultCal.add(Calendar.DAY_OF_MONTH, -1);
            dateString = DATE_FORMAT.format(resultCal.getTime());
            // 小时数加24
            int resultHour = resultCal.get(Calendar.HOUR_OF_DAY) + 24;
            // 拼接入睡时间
            dateString = dateString.substring(0,11) + resultHour + dateString.substring(13);
        }
        return dateString;
    }

    /**
     * 转换入睡时间
     * @param dateStr
     * @return
     */
    public static String convertSleepTime(String dateStr) {
        try {
            Date date = DATE_FORMAT.parse(dateStr);
            return convertSleepTime(date);
        } catch (ParseException e) {
            LOG.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 获取打卡日期
     * @param sleepTime
     * @return
     */
    public static Date getSleepDate(Date sleepTime) {
        Calendar sleepTimeCal = Calendar.getInstance();
        sleepTimeCal.setTime(sleepTime);
        // 当天中午12点
        Calendar middleCal = (Calendar) sleepTimeCal.clone();
        middleCal.set(Calendar.HOUR_OF_DAY, 12);
        middleCal.set(Calendar.MINUTE, 0);
        middleCal.set(Calendar.SECOND, 0);
        middleCal.set(Calendar.MILLISECOND, 0);
        // 如果入睡时间<中午12点，则记为前一天;否则就是当天
        Calendar sleepDateCal = (Calendar) sleepTimeCal.clone();
        if (sleepTime.before(middleCal.getTime())) {
            sleepDateCal.add(Calendar.DAY_OF_MONTH, -1);
        }
        return sleepDateCal.getTime();
    }


    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 2);
        System.out.println(DateUtil.convertSleepTime(cal.getTime()));
    }

}
